 #-*- coding: utf-8 -*-
#!/usr/bin/env python

#This is the FINAL version of the parameter finder. It finds the same photometric parameters as JP10.

import sys, glob, os, atpy, math, modelatm, param_finder_modules
import matplotlib.pyplot as plt

wavels='4500'
wavelf='4600'
resolution='2.3'

params_found = open('params_found.out','w')
#This section loads the van Loon data and at the moment takes a given LEID and extracts the row
tbl_overall = atpy.Table('data/vizier_votable.vot')
tbl_2MASS = atpy.Table('data/2MASS_vL07_matches.vot')
tbl_overall.sort('LEID')
#tbl = tbl.where((tbl.LEID == 6))
#Added so that we can use the brute force FeHs
params_found.write('LEID,RA,Dec,Bmag,J_2MASS,K_2MASS,Vmag,BmV,temp_BmV,VmK,temp_VmK,log_g,temp_round,grav_round,FeH,abundance,chi2_1,specfile,nS,flag\n')
counter=0
FeH_me=-1.49999
#this value was set to this so that some stars weren't lost that sit right on the cusp of being rejected due to the B-V
for i in range(len(tbl_overall)):
	using_BmV=0; no_B=0; flag="";
	BmV="";	Bmag=""; Vmag=""; M_V=""; VmK=""; temp_BmV=""; temp_VmK=""; log_g=""; temp=""; J_2MASS=""; K_2MASS=""
	best_parms=["","","","","",""]
	
	counter=counter+1; print counter
	
	LEID=tbl_overall.data['LEID'][i]
	RA=tbl_overall.data['_RAJ2000'][i]
	Dec=tbl_overall.data['_DEJ2000'][i]
	BmV=tbl_overall.data['B-V'][i]
	Bmag=tbl_overall.data['Bmag'][i]
	nS=tbl_overall.data['nS'][i]
	spFile=tbl_overall.data['spFile'][i]
	
	if Bmag!=Bmag or BmV!=BmV:
		flag=flag+"a"
	else:
		Vmag=Bmag-BmV-0.36 #This is the A(V)
		BmV=BmV-0.12 #This is done after the step above else you are doubly correcting
		temp_BmV=param_finder_modules.Alonso_BmV(BmV,FeH_me)
		if temp_BmV=="":
			flag=flag+"b"
		tbl = tbl_2MASS.where((tbl_2MASS.LEID == LEID))
		if len(tbl)==0:
			VmK=''
			flag=flag+"c"
		else:
			J_2MASS=tbl.data['j_m'][0]
			K_2MASS=tbl.data['k_m'][0]
			#The A(V) is unapplied since the 2MASS photometry has not been corrected
			[JmK,VmK]=param_finder_modules.TCS_transforms(J_2MASS,K_2MASS,Vmag+0.36)
			temp_VmK=param_finder_modules.Alonso_VmK(VmK,FeH_me)
			temp_JmK=param_finder_modules.Alonso_JmK(JmK,FeH_me)
		if temp_VmK=="":
			temp=temp_BmV
			using_BmV=1
			flag=flag+"d"
		else:
			using_BmV=0
			temp=temp_VmK
		
		M_V=Vmag-13.7
		if temp!="" and temp<5750:
			log_g=param_finder_modules.Alonso_logg(temp,FeH_me,M_V)
			if log_g=="":
				flag=flag+"e"
			if using_BmV!=1:
				#If there is more than one spectrum both are analyzed and the full line is reprinted
				for i in range(nS):
					best_parms=param_finder_modules.spectrum_fitter_fixed_temp(temp,resolution,wavels,wavelf,i,spFile)
					params_found.write(str(LEID)+','+str(RA)+','+str(Dec)+','+str(Bmag)+','+str(J_2MASS)+','+str(K_2MASS)+','+str(Vmag)+','+str(BmV)+','+str(temp_BmV)+','+str(VmK)+','+str(temp_VmK)+','+str(log_g)+','+str(best_parms[0])+','+str(best_parms[1])+','+str(best_parms[2])+','+str(best_parms[3])+','+str(best_parms[4])+','+str(best_parms[5])+','+str(nS)+","+flag+'\n')
			else:
				for i in range(nS):
					best_parms=param_finder_modules.spectrum_fitter_floating_temp(temp,resolution,wavels,wavelf,i,spFile)
					params_found.write(str(LEID)+','+str(RA)+','+str(Dec)+','+str(Bmag)+','+str(J_2MASS)+','+str(K_2MASS)+','+str(Vmag)+','+str(BmV)+','+str(temp_BmV)+','+str(VmK)+','+str(temp_VmK)+','+str(log_g)+','+str(best_parms[0])+','+str(best_parms[1])+','+str(best_parms[2])+','+str(best_parms[3])+','+str(best_parms[4])+','+str(best_parms[5])+','+str(nS)+","+flag+'\n')
		else:
			for i in range(nS):
					params_found.write(str(LEID)+','+str(RA)+','+str(Dec)+','+str(Bmag)+','+str(J_2MASS)+','+str(K_2MASS)+','+str(Vmag)+','+str(BmV)+','+str(temp_BmV)+','+str(VmK)+','+str(temp_VmK)+','+str(log_g)+','+str(best_parms[0])+','+str(best_parms[1])+','+str(best_parms[2])+','+str(best_parms[3])+','+str(best_parms[4])+','+str(best_parms[5])+','+str(nS)+","+flag+'\n')
		#params_found.write(str(LEID)+','+str(RA)+','+str(Dec)+','+str(Bmag)+','+str(J_2MASS)+','+str(K_2MASS)+','+str(Vmag)+','+str(BmV)+','+str(temp_BmV)+','+str(VmK)+','+str(temp_VmK)+','+str(JmK)+','+str(temp_JmK)+'\n')

	
